# -*- encoding: utf-8 -*-
from paddleocr import PaddleOCR, draw_ocr
import pyttsx3
import time
import rwlock
import threading

lock = threading.Lock()


class ImgOCR(object):
    def __init__(self):
        super().__init__()
        self.ocr = PaddleOCR(use_angle_cls=True, lang='ch')
        self.text = []
        self.engine = pyttsx3.init()

    def clean(self):
        self.text = []

    def speech(self):
        if len(self.text) <= 0:
            return
        for txt in self.text:
            self.engine.say(txt)
        self.engine.runAndWait()

    def show_text(self):
        if len(self.text) <= 0:
            return
        print(self.text)

    def process_img(self, img):
        if img is None:
            return

        with lock:

            print("1:", self.text)

            if len(self.text) > 0:
                self.speech()
                return

            result = self.ocr.ocr(img, cls=True)
            self.text = [line[1][0] for line in result]
            # print(self.text)
            self.speech()
